package com.zadetk.helpyou.base;

/**
 * Created by Zackv on 2018/4/2.
 */

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gyf.barlibrary.ImmersionBar;
import com.zadetk.helpyou.R;


public abstract class AbsActivity extends BaseActivity {

    public ImmersionBar immersionBar;
    protected Activity mActivity;
    protected LinearLayout mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mActivity = this;
        immersionBar = ImmersionBar.with(this);
        immersionBar.statusBarDarkFont(true, 0.2f);
        immersionBar.fitsSystemWindows(isfit());
        immersionBar.statusBarColor(getStatusBarColor());
        immersionBar.init();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏

        if (fullScreen()) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                    , WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        overridePendingTransition(R.anim.push_pic_left_in, R.anim.push_pic_left_out);
        final int vid = setView();

        super.onCreate(savedInstanceState);

        if (vid != 0) {
            LinearLayout layout = new LinearLayout(this);
            mToolbar = initToolbar(layout);
            if (mToolbar != null) {

                layout.setOrientation(LinearLayout.VERTICAL);

                View container = getLayoutInflater().inflate(vid, layout, false);

                layout.addView(mToolbar);

                layout.addView(container, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
                        , LinearLayout.LayoutParams.MATCH_PARENT));

                setContentView(layout);

            } else {
                setContentView(vid);
            }
            initBasic(savedInstanceState);
        }
    }

    public boolean isfit() {
        return true;
    }

    public LinearLayout initToolbar(ViewGroup parent) {
        LinearLayout toolbar = (LinearLayout) getLayoutInflater().inflate(R.layout.title_bar, parent, false);
        View back = toolbar.findViewById(R.id.iv_back);
        if (back != null) {
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
                }
            });
        }
        return toolbar;
    }

    public void setTopTitle(String title) {
        if (mToolbar != null) {
            TextView titleBar = (TextView) mToolbar.findViewById(R.id.tv_title);
            if (titleBar != null) {
                titleBar.setText(title);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Glide.with(this).pauseRequestsRecursive();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Glide.with(this).resumeRequestsRecursive();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.push_pic_right_in, R.anim.push_pic_right_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (immersionBar != null)
            immersionBar.destroy();
    }

    /**
     * 是否全屏
     *
     * @return true则全屏
     */
    public boolean fullScreen() {
        return false;
    }

    /**
     * 关联主界面
     */
    public abstract int setView();

    /**
     * 基础初始化
     */
    public abstract void initBasic(Bundle savedInstanceState);

    public int getStatusBarColor() {
        return R.color.colorWhite;
    }
}

